export default {
  data() {
    return {
      isAddingSpecialRule: false,
      parkSpecialChargeVoList: {
        ruleList: []
      },
      specialRuleItemInitData: null,
      specialRuleTestRules: {
        chargeName: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
        'parkSpecialChargeRepeatShareFreeVo.parkSpecialChargeRepeatShareFreeNaturalDayVo.freeValue': [{ required: true, message: '请输入免费时长', trigger: 'blur' }],
        'parkSpecialChargeServiceConditionVo.serviceConditionType': [{ required: true, message: '请选择使用条件', trigger: 'change' }],
        'parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopParkingDurationVo.maxValue': [{ required: true, message: '请输入封顶金额', trigger: 'blur' }]
      }
    };
  },
  methods: {
    setSpecialRuleInitData(val) {
      this.specialRuleItemInitData = {
        isShow: true,
        chargeName: '',
        chargeType: val ? val : 5,
        enableFlag: true,
        id: null,
        parkID: null,
        parkSpecialChargeFreeVo: {
          parkSpecialChargeDateTimeList: [
            {
              endTime: '',
              startTime: ''
            }
          ]
        },
        parkSpecialChargeAllDayTopVo: {
          institutionType: 0,
          parkSpecialChargeAllDayTopNaturalDayVo: {
            maxValue: '', //元(最多小数点后两位)
            startTime: '', //时间(00:00:00)
            parkSpecialChargeAllDayTopNaturalDaySubTimeVoList:[] 
          },
          parkSpecialChargeAllDayTopParkingDurationVo: {
            maxValue: ''
          }
        },
        parkSpecialChargeAllDayTotalVo: {
          addUpType: 0,
          addUpValue: 0,
          institutionType: 0
        },
        parkSpecialChargeAmountPrefrentialVo: {
          parkSpecialChargeDateTimeList: [
            {
              endTime: '',
              startTime: ''
            }
          ],
          prefrentialType: 0,
          prefrentialValue: 0
        },
        parkSpecialChargeTimePrefrentialVo: {
          parkSpecialChargeDateTimeList: [
            {
              endTime: '',
              startTime: ''
            }
          ],
          parkingTimePrefrentialMinutes: 0
        },
        parkSpecialChargeServiceConditionVo: {
          serviceConditionType: '',
          serviceConditionValue: []
        },
        parkSpecialChargeRepeatShareFreeVo: {
          institutionType: 1,
          parkSpecialChargeRepeatShareFreeParkingDurationVo: null,
          parkSpecialChargeRepeatShareFreeNaturalDayVo: {
            startTime: null,
            freeValue: ''
          }
        }
      };
    },
    addSpecialRule() {
      this.isAddingSpecialRule = true;
      this.setSpecialRuleInitData();
    },
    serviceConditionTypeChange(val) {
      if (val === 1) {
        this.specialRuleItemInitData.parkSpecialChargeServiceConditionVo.serviceConditionValue = '';
      }

      if (val === 3) {
        this.specialRuleItemInitData.parkSpecialChargeServiceConditionVo.serviceConditionValue = [];
      }
    },
    listItemServiceConditionTypeChange(val, item) {
      if (val === 1) {
        item.parkSpecialChargeServiceConditionVo.serviceConditionValue = '';
      }

      if (val === 3) {
        item.parkSpecialChargeServiceConditionVo.serviceConditionValue = [];
      }
    },
    async addSpecialRuleToList() {
      let vaild = await this.testAddSpecialRule();
      if (vaild) {
        let saveData = JSON.parse(JSON.stringify(this.specialRuleItemInitData));
        if(saveData.chargeType === 1 && saveData.parkSpecialChargeAllDayTopVo.institutionType === 1){
          let listTmp = saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList
          if(this.testTimeRange24Hours(listTmp)){
            this.showToast('封顶时段不能重合且组成24小时闭环',0)
            return
          }
          saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime = saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime ? saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime+':00' : '00:00:00';
        }

        this.parkSpecialChargeVoList.ruleList.push(saveData);
        this.specialRuleItemInitData = null;
        this.isAddingSpecialRule = false;
      }
    },
    testAddSpecialRule() {
      return new Promise((resolve, reject) => {
        this.$refs['specialRuleItemInitData'].validate(valid => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },
    delSpecialRuleItem(list, index) {
      list.splice(index, 1);
    },
    //添加子封顶
    addNaturalDayTopItem(item){
        let startTime = item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime
        let list = item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList;
        if(list.length==0){
          list.push(
            {
              startTime: startTime, //开始时间
              endTime: '', //结束时间
              maxValue: '', //金额(最多小数点后两位)
            },
            {
              startTime: '', //开始时间
              endTime: startTime, //结束时间
              maxValue: '', //金额(最多小数点后两位)
            }
          )
        }else if(list.length>=2){
          let obj = {
            startTime: '', //开始时间
            endTime: '', //结束时间
            maxValue: '', //金额(最多小数点后两位)
          };
          list.splice(list.length-1,0,obj)
        }
    },
     //自然日 起始时刻
     naturalDayStartTime(val){
      let len =  this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList.length
      this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList[0].startTime = val
      this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList[len-1].endTime = val
    },
    // 自然日 起始时刻-单个规则
    naturalDayStartTimeForSingle(item){
      let startTime = item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime
      let subTopList = item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList
      subTopList[0].startTime = startTime
      subTopList[subTopList.length-1].endTime = startTime
    },
    // 删除 自然日 子封顶
    deleteSubTopItem(item,index){
      let list = item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList
      if(list.length<=2){
        list.splice(0,list.length)
        return;
      }
      if(index==0){
        list[index+1].startTime = item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime
      }else if(index==list.length-1){
        list[index-1].endTime = item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.endTime
      }
      list.splice(index,1)
    },
    testTimeRange24Hours(list) {
      let flag = false;
      if (list.length) {
        const day_rang = 24 * 60 * 60 * 1000;
        let cur_day_rang = 0;
        list.forEach(item => {
          item.startTime = item.startTime ? (item.startTime.length==8 ? item.startTime : item.startTime+':00')  : ''
          item.endTime = item.endTime ? (item.endTime.length==8 ? item.endTime : item.endTime+':00')  : ''
          let rang;
          if (this.getTime(item.startTime) >= this.getTime(item.endTime)) {
            rang = this.getTime(item.endTime) + day_rang - this.getTime(item.startTime);
          } else {
            rang = this.getTime(item.endTime) - this.getTime(item.startTime);
          }
          cur_day_rang += rang;
        });
        if (day_rang !== cur_day_rang) {
          flag = true;
        }
      }
      return flag;
    },
    getTime(time) {
      const d = new Date();
      return new Date([d.getFullYear(), d.getMonth() + 1, d.getDate()].join('/') + ' ' + time).getTime();
    },
  },
  watch: {
    'specialRuleItemInitData.chargeType'(val) {
      this.setSpecialRuleInitData(val);
    }
  }
};
